home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / dir_make.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  2KB  |  90 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include "msdos.h"
  4.  
  5. extern int dir_start, dir_len, clus_size, dir_entries, fat_error, clus_size;
  6. extern unsigned int num_clus, end_fat, last_fat;
  7. extern long dir_chain[MAX_DIR_SECS];
  8. extern unsigned char *dir_buf;
  9.  
  10. /*
  11.  * Make a subdirectory grow in length.  Only subdirectories (not root)
  12.  * may grow.  Returns a 0 on success, 1 on failure (disk full), or -1
  13.  * on error.
  14.  */
  15.  
  16. int
  17. dir_grow(fat)
  18. unsigned int fat;
  19. {
  20.     int i, num, buflen, new;
  21.     long sector;
  22.     char *memset(), *realloc();
  23.     unsigned char *offset, tbuf[MAX_CLUSTER];
  24.     unsigned int next, last, next_fat(), fat_decode();
  25.     void perror(), exit(), disk_write(), disk_read();
  26.  
  27.     last = next_fat(0);
  28.     if (last == 1)
  29.         return(1);
  30.  
  31.     /* CONSTCOND */
  32.     while (1) {
  33.         next = fat_decode(fat);
  34.         if (next == 1) {
  35.             fprintf(stderr, "dir_grow: FAT problem\n");
  36.             fat_error++;
  37.             return(-1);
  38.         }
  39.                     /* end of cluster chain */
  40.         if (next >= last_fat)
  41.             break;
  42.         fat = next;
  43.     }
  44.                     /* mark the end of the chain */
  45.     fat_encode(fat, last);
  46.     fat_encode(last, end_fat);
  47.                     /* zero the buffer */
  48.     buflen = clus_size * MSECTOR_SIZE;
  49.     memset((char *) tbuf, '\0', buflen);
  50.  
  51.                     /* write the cluster */
  52.     sector = (long) (last - 2) * clus_size + dir_start + dir_len;
  53.     disk_write(sector, tbuf, buflen);
  54.  
  55.                     /* fix up the globals.... */
  56.     num = dir_entries / 16;
  57.     dir_entries += clus_size * 16;
  58.     for (i = 0; i < clus_size; i++)
  59.         dir_chain[num + i] = sector + i;
  60.  
  61.                     /* fix up dir_buf.... */
  62.     new = num + clus_size;
  63.     dir_buf = (unsigned char *) realloc((char *) dir_buf, (unsigned int) new * MSECTOR_SIZE);
  64.     if (dir_buf == NULL) {
  65.         perror("dir_grow: realloc");
  66.         exit(1);
  67.     }
  68.     offset = dir_buf + (num * MSECTOR_SIZE);
  69.     disk_read(dir_chain[num], offset, clus_size * MSECTOR_SIZE);
  70.     return(0);
  71. }
  72.  
  73. /*
  74.  * Returns next free cluster or 1 if none are available.
  75.  */
  76.  
  77. unsigned int
  78. next_fat(last)
  79. unsigned int last;
  80. {
  81.     register unsigned int i;
  82.     unsigned int fat_decode();
  83.  
  84.     for (i = last + 1; i < num_clus + 2; i++) {
  85.         if (!fat_decode(i))
  86.             return(i);
  87.     }
  88.     return(1);
  89. }
  90.